<?php

namespace app\admin\controller\shopro\store;

use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use Exception;


/**
 * 门店
 *
 * @icon fa fa-circle-o
 */
class Recharge extends Backend
{

    /**
     * Store模型对象
     * @var \app\admin\model\shopro\store\Recharge
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\shopro\store\Recharge;
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        $ids = $this->request->get('ids');
        if ($this->request->isAjax()) {
            $search = $this->request->get('search');
            $wheres = [];
            if(!empty($ids)){
                $wheres = ['sm.store_id'=>$ids];
            }
            $searchwhere = [];
            if(!empty($search)){
                $searchwhere = ['store.name', 'LIKE', "%$search%"];
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
//            $total = $this->model->alias('sm')
//                ->join('shopro_store store','sm.store_id=store.id')
//                ->where($where)->where($wheres)
//                ->whereOr('store.name', 'LIKE', "%$search%")
//                ->order('sm.'.$sort, $order)
//                ->count();

            $list = $this->model->alias('sm')
                ->field('sm.*,store.name,u.nickname uname,a.username aname')
                ->join('shopro_store store','sm.store_id=store.id')
                ->join('user u','u.id=sm.user_id','left')
                ->join('admin a','a.id=sm.admin_id','left')
                ->where($where)
                ->where($wheres)
                ->whereOr($searchwhere)
                ->order('sm.'.$sort, $order)
                ->limit($offset, $limit)
                ->paginate();
//            $list = collection($list)->toArray();
//            $result = array("total" => $total, "rows" => $list);

            return $this->success('门店余额列表', null, $list);
        }
        $this->assign('ids',$ids);
        return $this->view->fetch();
    }
}
